package cn.hwadee.muying.controller;


import cn.hwadee.muying.commons.constant.ConstantRole;
import cn.hwadee.muying.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import cn.hwadee.muying.commons.base.BaseController;
import cn.hwadee.muying.commons.jqgrid.GridResult;
import cn.hwadee.muying.commons.page.MyPage;
import org.springframework.ui.Model;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.hwadee.muying.service.ContractService;
import cn.hwadee.muying.entity.Contract;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.shiro.authz.annotation.Logical;

/**
* <p>
* 服务合同 前端控制器
*
* </p>
*
* @author Mankind
* @since 2018-08-06
*/
@Controller
@RequestMapping("/sys/contract")
public class ContractController extends BaseController {
    @Autowired
    private ContractService contractService;

    @Autowired
    private UserService userService;

    @ModelAttribute("contract")
    public Contract get(@RequestParam(required = false)Long id) throws Exception{
        if(id != null && id > 0){
            return contractService.selectById(id);
        }else{
            return new Contract();
        }
    }

    @RequiresPermissions("sys:contract:list")
    @RequestMapping(value="/list",produces = "text/html;charset=utf-8",method = RequestMethod.GET)
    public String list(Model model) throws Exception{
        model.addAttribute("nurseryTeachers",userService.selectAllByRole(ConstantRole.ROLE_NURSERY_TEACHER));
        model.addAttribute("users",userService.selectAllByRole(ConstantRole.ROLE_CUSTOM));
        model.addAttribute("nurseryConsultants",userService.selectAllByRole(ConstantRole.ROLE_NURSERY_CONSULTANT));
        return "/biz/contract/list";
    }

    @ResponseBody
    @RequiresPermissions("sys:contract:list")
    @RequestMapping(value="/list.json",produces = "application/json;charset=utf-8")
    public GridResult list(Long userId, Long nurseryTeacherId, Long nurseryConsultantId, Date startDate, Date  endDate, Long status, String keyword) throws Exception{
        MyPage<Contract> page = getPage();
        //System.out.println( userId + " " + nurseryTeacherId + " " + nurseryConsultantId + " " + startDate +" "+ endDate +" "+ status);
        page = contractService.list(page,getCurrentUser(),userId,nurseryTeacherId,nurseryConsultantId,startDate,endDate,status,keyword);
        GridResult result = new GridResult(page.getCurrent(),page.getPages(),page.getCurrent(),page.getRecords());
        return result;
    }

    /**
     * 跳转查看服务合同界面
     * @param contract
     * @param model
     * @return
     * @throws Exception
     */
    @RequiresPermissions(value={"sys:contract:list"},logical=Logical.OR)
    @RequestMapping(value = {"/view"}, produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String view(@ModelAttribute Contract contract,Model model) throws Exception{
        model.addAttribute("contract",contract);
        model.addAttribute("nurseryTeachers",userService.selectById(contract.getNurseryTeacherId()));
        model.addAttribute("users",userService.selectById(contract.getUserId()));
        model.addAttribute("nurseryConsultants",userService.selectById(contract.getNurseryConsultantId()));
        return "/biz/contract/view";
    }

    /**
     * 跳转添加/编辑员工等级界面
     * @param contract
     * @param model
     * @return
     * @throws Exception
     */
    @RequiresPermissions(value={"sys:contract:edit","sys:contract:add"},logical=Logical.OR)
    @RequestMapping(value = {"/edit","/add"}, produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String add(@ModelAttribute Contract contract,Model model) throws Exception{
        model.addAttribute("contract",contract);
        model.addAttribute("nurseryTeachers",userService.selectAllByRole(ConstantRole.ROLE_NURSERY_TEACHER));
        model.addAttribute("users",userService.selectAllByRole(ConstantRole.ROLE_CUSTOM));
        model.addAttribute("nurseryConsultants",userService.selectAllByRole(ConstantRole.ROLE_NURSERY_CONSULTANT));
        return "/biz/contract/edit";
    }

    /**
     * 处理更新服务合同的请求
     * @param contract
     * @return
     * @throws Exception
     */
    @RequiresPermissions(value={"sys:contract:edit","sys:contract:add"},logical=Logical.OR)
    @RequestMapping(value = {"/edit"}, produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> edit(@ModelAttribute Contract contract){
        Map<String,Object> result = new HashMap();
        contract.setOrgId(getCurrentUser().getOrgId());
        try {
        if(contract.getId() != null && contract.getId() > 0){
            contractService.updateById(contract);
        }else {
            contractService.insert(contract);
        }
        result.put("status",200);
        result.put("msg","服务合同信息保存成功");
        }catch (Exception e){
        e.printStackTrace();
        result.put("status",500);
        result.put("msg","服务合同信息保存失败");
        }
        return result;
    }

    /**
     * 处理结束服务合同的请求
     * @param id
     * @return
     * @throws Exception
     */
    @RequiresPermissions("sys:contract:finishContract")
    @RequestMapping(value = "/finishContract", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> finishContract(Long id) throws Exception{
        Map<String,Object> result = new HashMap();
        try{
            if(id != null && id > 0){
                contractService.finishContract(id);
                result.put("status",200);
                result.put("msg","合同结束成功");
            }else {
                result.put("status",500);
                result.put("msg","请选择要结束的合同");
            }
        }catch (Exception e){
            result.put("status",500);
            result.put("msg","请选择要结束的合同");
            e.printStackTrace();
        }
        return result;
    }

}
